/*
** wifi
*  接口文档：https://device.harmonyos.com/cn/docs/documentation/DevicePartner-References/obtain-cached-devices-tatus-set-0000001077308386
*/
import utils from "../utils/Utils";
import storeManage from "../store/index";
export default {

    /**
    * 是否存在hilink
    */
    isExsitHilink() {
        if (!window.hilink) {
            console.log("WifiManage => no hilink ");
            return false;
        } else {
            return true;
        }
    },

    /**
    * 获取设备事件通知
    * 
    * @param {Function} callback - 回调
    */
    getDeviceEvent(callback) {
        if (this.isExsitHilink()) {
            console.log("WifiManage => getDeviceEvent");
            window.deviceEventCallback = (res) => {
                let result = JSON.parse(res);
                console.log("WifiManage => getDeviceEvent " + result);
                storeManage.commit("wifi/setDeviceEventRes", result);
                if (callback) callback(result);
            };
        }
    },

    /**
    * 获取设备状态信息
    *
    * @param {string} devid 请取"0"，表示当前设备，暂不支持其它取值 
    * @param {string} sid  为该设备的service id，取值参见Profile定义
    * @param {string} body 需要获取的设备characteristicName
    * @param {Function} callback - 回调
    */
    getDevInfo(devid, sid, body, callback) {
        utils.debounce(() => {
            if (this.isExsitHilink()) {
                console.log("WifiManage => getDevInfo");
                window.getDevInfoCallback = (res) => {
                    let result = JSON.parse(res);
                    console.log("WifiManage => getDevInfo " + result);
                    storeManage.commit("wifi/setDevInfoRes", result);
                    if (callback) callback(result);
                };
                hilink.getDevInfo(devid, sid, JSON.stringify(body), "getDevInfoCallback");
            }
        }, 100);
    },

    /**
    * 设置标题栏是否可见
    * 
    * @param {boolean} visible - 是否显示
    * @param {Function} callback - 回调
    */
    setTitleVisible(visible, callback) {
        if (this.isExsitHilink()) {
            console.log("WifiManage => setTitleVisible " + visible);
            window.setTitleVisibleCallback = (res) => {
                let result = JSON.parse(res);
                console.log("WifiManage => setTitleVisible " + result);
                if (callback) callback(res);
            };
            window.hilink.setTitleVisible(visible, "setTitleVisibleCallback");
        }
    },

    /**
    * 设置标题栏是否可见
    * 
    * @param {boolean} colorFlag - true，标题栏及状态栏文字白色，false标题栏及状态栏文字黑色
    * @param {string} color ARGB背景色值，目前已无作用
    * @param {Function} callback - 回调
    */
    modifyTitleBar(colorFlag, color, callback) {
        if (this.isExsitHilink()) {
            console.log("WifiManage => modifyTitleBar ");
            window.modifyTitleBarCallback = (res) => {
                let result = JSON.parse(res);
                console.log("WifiManage => modifyTitleBar " + result);
                if (callback) callback(res);
            };
            window.hilink.modifyTitleBar(colorFlag, color, "modifyTitleBarCallback");
        }
    },

    /**
    * 获取设备版本信息
    * 
    * @param {Function} callback - 回调
    */
    getDeviceVersion(callback) {
        if (this.isExsitHilink()) {
            console.log("WifiManage => getDeviceVersion ");
            window.getVersionCallback = (res) => {
                let result = JSON.parse(res);
                console.log("WifiManage => getDeviceVersion " + result);
                storeManage.commit("wifi/setDeviceVersionRes", result);
                let isIOS = result.os === 'Android' ? false : true;
                storeManage.commit("setIsIos", isIOS);
                if (callback) callback(result);
            };
            window.hilink.getVersion("getVersionCallback");
        }
    },

    /**
    * 获取缓存设备状态全集
    * 
    * @param {string} devid - 请取"0"，表示当前设备，暂不支持其它取值
    * @param {string} body 可选，见下描述
    * @param {Function} callback - 回调
    */
    getDevCacheAll(devid, body, callback) {
        if (this.isExsitHilink()) {
            console.log("WifiManage => getDevCacheAll ");
            window.getDevCacheAllCallback = (res) => {
                let result = JSON.parse(res);
                console.log("WifiManage => getDevCacheAll " + result);
                storeManage.commit("wifi/setDevCacheAllRes", result);
                if (callback) callback(result);
            };
            window.hilink.getDevCacheAll(devid, JSON.stringify(body), "getDevCacheAllCallback");
        }
    },

    /**
    * 跳转到设备信息的界面
    * 
    * @param {string} uri - 跳转连接
    * @param {Function} callback - 回调
    */
    jumpTo(uri, callback) {
        if (this.isExsitHilink()) {
            console.log("WifiManage => jumpTo " + uri);
            window.jumpToCallback = (res) => {
                if (callback) callback(res);
            };
            window.hilink.jumpTo(uri, "jumpToCallback");
        }
    },

    /**
    * 退出设备页面
    */
    finishDeviceActivity() {
        if (this.isExsitHilink()) {
            console.log("WifiManage => finishDeviceActivity ");
            window.hilink.finishDeviceActivity();
        }
    },

    /**
    * 下发命令到设备（单属性下发）
    * 
    * @param {string} devid - 请取"0"，表示当前设备，暂不支持其它取值
    * @param {string} body - 需要设置的设备characteristicName以及要设置的value
    * @param {Function} callback - 回调
    */
    setDeviceInfo(devid, body, callback) {
        if (this.isExsitHilink()) {
            console.log("WifiManage => setDeviceInfo ");
            window.setDeviceInfoCallback = (res) => {
                let result = JSON.parse(res);
                console.log("WifiManage => setDeviceInfo " + result);
                if (callback) callback(result);
            };
            window.hilink.setDeviceInfo(devid, JSON.stringify(body), "setDeviceInfoCallback");
        }
    },

    /**
    * 下发命令到设备（带prodId方式）
    * 
    * @param {string} devid - 请取"0"，表示当前设备，暂不支持其它取值
    * @param {string} body - 需要设置的设备characteristicName以及要设置的value
    * @param {Function} callback - 回调
    */
    setDeviceInfoWithProdId(devid, body, callback) {
        if (this.isExsitHilink()) {
            console.log("WifiManage => setDeviceInfoWithProdId ");
            window.setDeviceInfoWithProdIdCallback = (res) => {
                let result = JSON.parse(res);
                console.log("WifiManage => setDeviceInfoWithProdId " + result);
                if (callback) callback(result);
            };
            window.hilink.setDeviceInfoWithProdId(devid, JSON.stringify(body), "setDeviceInfoWithProdIdCallback");
        }
    },

    /**
    * 下发命令到设备（长按控制）
    * 
    * @param {string} devid - 请取"0"，表示当前设备，暂不支持其它取值
    * @param {string} body - 需要设置的设备characteristicName以及要设置的value
    * @param {boolean} isLongPress - true：长按命令开始执行，false：长按命令结束
    * @param {Function} callback - 回调
    */
    setLongPressDeviceInfo(devid, body, isLongPress, callback) {
        if (this.isExsitHilink()) {
            console.log("WifiManage => setLongPressDeviceInfo ");
            window.setDeviceInfoWithProdIdCallback = (res) => {
                let result = JSON.parse(res);
                console.log("WifiManage => setLongPressDeviceInfo " + result);
                if (callback) callback(result);
            };
            window.hilink.setLongPressDeviceInfo(devid, JSON.stringify(body),
                "setDeviceInfoWithProdIdCallback", isLongPress);
        }
    },

    /**
    * 下发命令到设备（循环控制）
    * 
    * @param {string} devid - 请取"0"，表示当前设备，暂不支持其它取值
    * @param {string} serivceId - 控制设备的命令服务id
    * @param {string} body - 需要设置的设备characteristicName以及要设置的value
    * @param {string} loopFlag - 是否重复循环标识 ，"1"表示是， "0" 表示否。
    * @param {string} intervalTime - 间隔时间
    * @param {Function} callback - 回调
    */
    setDeviceServiceLoop(devid, serivceId, body, loopFlag, intervalTime, callback) {
        if (this.isExsitHilink()) {
            console.log("WifiManage => setDeviceServiceLoop ");
            window.setDeviceServiceLoopCallback = (res) => {
                let result = JSON.parse(res);
                console.log("WifiManage => setDeviceServiceLoop " + result);
                if (callback) callback(result);
            };
            window.hilink.setDeviceServiceLoop(devid, serivceId, JSON.stringify(body),
                loopFlag, intervalTime, "setDeviceServiceLoopCallback");
        }
    }
}